Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typedconverter

Package Overview
Dependencies
Maintainers
1
Versions
193
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typedconverter

Convert object into classes match with TypeScript type annotation

  • 1.0.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

TypedConverter

Convert object into classes match with TypeScript type annotation

Build Status Coverage Status Greenkeeper badge

Convert Primitive Type

import { convert } from "typedconverter";

const numb = await convert("12345", { type: Number }) //return number 12345
const numb = await convert("YES", { type: Boolean }) //return true
const numb = await convert("2019-2-2", { type: Date }) //return date 1/1/2019

Specify type on configuration

Expected type can be specified in the configuration, than you can omit expected type on the second parameter of the convert function. Useful when you want to covert several times without specifying expected type.

import createConverter from "typedconverter";

const convert = createConverter({type: Number})
const numb = await convert("12345")
const numb1 = await convert("-12345")
const numb2 = await convert("12345.123")

Convert custom class

TypedConvert uses tinspector to get type metadata, so it aware about TypeScript type annotation.

import {convert} from "typedconverter";
import reflect from "tinspector"


@reflect.parameterProperties()
class AnimalClass {
    constructor(
        public id: number,
        public name: string,
        public deceased: boolean,
        public birthday: Date
    ) { }
}
//return instance of AnimalClass with appropriate properties type
const data = await convert({ 
    id: "200", 
    name: "Mimi", 
    deceased: "ON", 
    birthday: "2018-2-2" }, 
    { type: AnimalClass }) 

Convert Array

Convert into array by providing array of type in the expected type.

import {convert} from "typedconverter";

const numb = await convert(["1", "2", "-3"], { type: [Number] })

Convert Child Array

Nested child array need to be decorate for TypeScript added design data type

import {convert} from "typedconverter";

@reflect.parameterProperties()
class Tag {
    constructor(
        public name: string,
    ) { }
}

@reflect.parameterProperties()
class Animal {
    constructor(
        public name: string,
        @reflect.array(Tag)
        public tags: Tags
    ) { }
}

//tags is instance of Tag class
const numb = await convert({name: "Mimi", tags: [{name: "Susi"}, {name: "Lorem"}]}, { type: Animal })

Guess Array Element

Useful when converting data from url encoded, where single value could be a single array.

const b = await convert("1", { type: [Number], guessArrayElement: true }) //ok [1]

Visitors

Visitors executed after conversion process traverse through properties / array element. Invocation can be multiple and run in sequence the last sequence will execute the converter. Visitors work like Plumier middleware

Signature of Visitor is like below:

type Visitor = (invocation: VisitorInvocation) => VisitorResult

Visitor is a function receive two parameters value and invocation.

  • invocation next invocation

Example:

import createConverter, { Result, VisitorInvocation } from "typedconverter"

const olderThanEightTeen = (i: VisitorInvocation) => {
    if (i.type === Number && i.value < 18)
        return Result.error(i.path, "Must be older than 18")
    else
        return i.proceed()
}

const convert = createConverter({ type: Number, visitors: [olderThanEightTeen] })
const result = convert("40") // { value: 40 }
const other = convert("12") // { issues: [{path: "", messages: ["Must be older than 18"]}]  }

Keywords

FAQs

Package last updated on 02 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc